package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.PrisTeam1UserAccount;

import manager.CarManager;

/**
 * Servlet implementation class newCarServlet
 */
@WebServlet("/newCarServlet")
public class NewCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewCarServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String brand = request.getParameter("brand");
		String model = request.getParameter("model");
		String pr = request.getParameter("price");
		int price = Integer.parseInt(pr);
		String fuelType = request.getParameter("fuelType");
		String y = request.getParameter("year");
		Integer year = Integer.parseInt(y);
		String p = request.getParameter("power");
		Integer power = Integer.parseInt(p);
		String k = request.getParameter("kilometers");
		Integer kilometers = Integer.parseInt(k);
		PrisTeam1UserAccount user = (PrisTeam1UserAccount) request.getSession()
				.getAttribute("username");
		CarManager cm = new CarManager();
		String msg;
		boolean ok = cm.createAndStoreCar(user, brand, model, price, fuelType,
				year, power, kilometers);
		if (ok) {
			response.sendRedirect("web/ads.jsp");
		} else {
			msg = "Entry has NOT been succesfull, please try again !!!";
			request.getSession().setAttribute("message", msg);
			response.sendRedirect("web/addAd.jsp");
		}
	}

}
